"use strict";

/**
 * @ngdoc directive
 * @name jgappApp.directive:relamodule
 * @description
 * # relamodule
 */
angular
  .module("jgappApp")
  .directive("relaModule", function(Util, $location) {
    return {
      scope: {
        param: "=param",
        taskid: "="
      },
      template:
        '<div class="list-item icon_more">' +
        '<div class="item-label">关联模块</div>' +
        '<label class="flow-content">' +
        '<select ng-model="param.relatedModuleName" ng-options="item.key as item.key for item in relmodlist" ng-change="modulechange()"></select>' +
        "</label>" +
        "</div>" +
        '<div class="list-item icon_more" ng-click="chooseinfo()">' +
        '<div class="item-label">模块编号</div>' +
        '<div class="flow-content" ng-class="{cd9:!param.moduleNo}">{{param.moduleNo||"点击选择"}}</div>' +
        "</div>",
      restrict: "AE",
      link: function postLink(scope, element, attrs) {
        console.log(scope.param);
        scope.chooseinfo = function() {
          // $routeParams.id通过id确定唯一回显id
          if (!scope.param.relatedModule || scope.param.relatedModule == "无") {
            Util.Toast("请选择关联模块");
            return;
          }
          Util.savaParamForReview(scope.param, scope.taskid);
          // 里面包含了title
          var relModuleObj = {
            selectTab: scope.param.relatedModule,
            selectTabContent: scope.param.relatedModuleName
          };
          Util.savaDataforsearch(relModuleObj);
          var functionId =-1;
          if (scope.param.relatedModuleName) {
            functionId = scope.relmodlist.filter(function(item) {
              return item.key == scope.param.relatedModuleName;
            })[0]["flag"];
          }
          $location.path("finance/choosepeople" + "/" + functionId);
        };
        scope.showrelModule = function() {
          Util.getModuleRel(Util[attrs.rel], function(data) {
            scope.relmodlist = data.data;
          });
        };
        scope.modulechange = function() {
          scope.param.relatedModule = Util.getValueFromkey(
            scope.relmodlist,
            scope.param.relatedModuleName
          );

          scope.param.moduleNo = "";
        };
        scope.showrelModule();
      }
    };
  })
  .directive("remarkList", function(
    Util,
    openApi,
    FileService,
    DataService,
    OpenFile
  ) {
    return {
      scope: {
        dataid: "=dataid",
        formKey: "=formKey"
      },
      template:
        '<div class="list table-list" ng-show="data.length">' +
        '<div class="table-item" ng-repeat="item in data">' +
        '<div class="list-item">' +
        '<div class="item-label">追加人</div>' +
        '<div class="flow-content">{{item.lastOperator}}</div>' +
        "</div>" +
        '<div class="list-item">' +
        '<div class="item-label">追加时间</div>' +
        '<div class="flow-content">{{item.createDate}}</div>' +
        "</div>" +
        '<div class="list-item">' +
        '<div class="item-label">内容</div>' +
        '<div class="flow-content" ng-bind-html="item.memo|parsetohtml"></div>' +
        "</div>" +
        '<div class="list-item">' +
        '<div class="item-label">附件</div>' +
        '<div class="flow-content c-blue">' +
        '<span ng-click="goDetail(item.attachment)" ng-hide="item.attachment|ifimg">{{item.attachment|fileName}}</span>' +
        '<img ng-click="goDetail(item.attachment)" ng-src="{{item.attachment|showimg}}" ng-if="item.attachment|ifimg" />' +
        "</div>" +
        "</div>" +
        "</div>",
      restrict: "AE",
      link: function postLink(scope, element, attrs) {
        scope.getRemarklist = function() {
          DataService.getData(openApi.otherDatamemo, {
            dataId: scope.dataid,
            formKey: scope.formKey
          }).then(function(data) {
            scope.data = data;
          });
        };
        scope.goDetail = function(fileData) {
          if (Util.isImg(fileData)) {
            FileService.showfile(fileData);
          } else if (Util.isFile(fileData)) {
            OpenFile.open(fileData);
          } else {
            Util.Toast("移动端不支持预览");
          }
        };
        scope.$watch("formKey", function(newvalue) {
          if (newvalue) {
            scope.getRemarklist();
          }
        });
      }
    };
  })
  .directive("remarkInput", function($location, Util, localStorageService) {
    return {
      scope: {
        uid: "=uid"
      },
      template:
        '<div class="section rel-list-label mt10" ng-click="goRemark()"><div class="info-title">追加备注</div></div>',
      replace: true,
      restrict: "AE",
      link: function postLink(scope, element, attrs) {
        scope.goRemark = function() {
          Util.setRelMoNum(1);
          $location.path("remark/index/" + scope.uid);
        };
      }
    };
  });
